package com.ctshk.common.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;

public class HktOrderUtil {

    public static void main(String[] args) throws Exception {
//        Map<String,String> map = new HashMap<>();
//        HktOrderUtil.getTickets(map);
    	Object obj = getLocations();
    	FileUtils.write(new File("D://aa.txt"), JSON.toJSONString(obj));
    }


    /**
     * 查询所有路线列表
     */
    public static List<Map> getRoutes() throws Exception{
        Map<String,Object> result = HktBusUtil.getRoutes();
        Map routes = (Map)result.get("routes");
        List<Map> route =  (List<Map>)routes.get("route");
        return route;
    }

    /**
     * 查询所有站点列表
     */
    public static List<Map<String, Object>> getLocations() throws Exception{
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map> routMap = getRoutes();
        if(!CollectionUtils.isEmpty(routMap)){
            for(Map map : routMap){
                Map<String,String> paramMap = new HashMap<>();
                paramMap.put("routeId",(String)map.get("routeId"));
                Map<String,Object> result = HktBusUtil.getLocations(paramMap);

                Map locations = new HashMap();
                Object object =  result.get("locationss");
                if(object instanceof Map){
                    locations = ((Map)object);
                }
                List<Map> location = (List<Map>)locations.get("locations");
                if(!CollectionUtils.isEmpty(location)){
                	Map<String, Object> resultMap = new HashMap<>();
                	resultMap.put("toCityName", map.get("toCityName"));
                	resultMap.put("fromCityName", map.get("fromCityName"));
                	resultMap.put("toCityId", map.get("toCityId"));
                	resultMap.put("fromCityId", map.get("fromCityId"));
                	resultMap.put("fromStations", map.get("fromStations"));
                	resultMap.put("toStations", map.get("toStations"));
                	resultMap.put("stopsMap", location);
                	
                    list.add(resultMap);
                }
            }
        }
        return list;
    }



    /**
     * 查询所有班次
     * @return
     * @throws Exception
     */
    public static List<Map> getAllSiteList() throws Exception{
        //线路查询
        try {
            List<Map> shift = new ArrayList<>();
            List<Map> routeMap = getRoutes();
            if(CollectionUtils.isEmpty(routeMap)){
                return null;
            }
            for (Map map : routeMap) {
                Map<String, String> paramMap2 = new HashMap<>();
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
                String departureDate = sdf2.format(new Date());
                paramMap2.put("beginCityArea", (String) map.get("fromCityId"));
                paramMap2.put("endCityArea", (String) map.get("toCityId"));
                paramMap2.put("departure_date", departureDate);
                paramMap2.put("isReturn", "0");
                paramMap2.put("currency_id", "1");
                Map<String, Object> result2 = HktBusUtil.getDeparturesZL(paramMap2);
                Map runs = (Map) result2.get("runs");
                Object object = runs.get("run");

                List<Map> run = new ArrayList<>();
                if(object instanceof Map){
                    run.add((Map)object);
                }else if(object instanceof List){
                    run = (List)object;
                }
                if(!CollectionUtils.isEmpty(run)){
                    for(Map runMap : run){
                        runMap.put("fromCityId",map.get("fromCityId"));
                        runMap.put("fromCityName",map.get("fromCityName"));
                        runMap.put("toCityId",map.get("toCityId"));
                        runMap.put("toCityName",map.get("toCityName"));
                    }
                }
                shift.addAll(run);
            }
            return shift;
        }catch(Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }




    /**
     * 查询班次查询
     */
    public static void getDeparturesZL() throws Exception{
        Map<String,String> paramMap2 = new HashMap<>();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String departureDate = sdf2.format(new Date());
        paramMap2.put("beginCityArea","3");
        paramMap2.put("endCityArea","1");
        paramMap2.put("departure_date","2021-03-24");
        paramMap2.put("isReturn","0");
        paramMap2.put("currency_id","1");

        Map<String,Object> result2 = HktBusUtil.getDeparturesZL(paramMap2);
        System.out.println("result2:"+result2);
    }

    /**
     * 查询所有班次
     */
    public static void getBusLines() throws Exception{
        Map<String,String> paramMap3 = new HashMap<>();
        paramMap3.put("route_id","1");
        paramMap3.put("departure_date","2021-03-24");
        paramMap3.put("currency_id","1");

        Map<String,Object> result3 = HktBusUtil.getBusLines(paramMap3);
        System.out.println(result3);
    }

    /**
     * 锁票-单程提交订单
     */
    public static  Map<String,Object> reservations(Map<String,String> paramMap) throws Exception{
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf2.format(new Date());
        paramMap.put("currency_id","1");
        paramMap.put("departure_date",date);
        paramMap.put("priceType","wechat");
        Map<String,Object> result = HktBusUtil.reservations(paramMap);
        Map reservationsResponseMap = (Map)result.get("reservationsResponse");
        return reservationsResponseMap;
    }


    /**
     * 下单
     */
    public static  Map<String,Object> getTickets(Map<String,String> paramMap) throws Exception{
        paramMap.put("cash_price","0");
        Map<String,Object> result = HktBusUtil.getTickets(paramMap);
        return result;
    }


    /**
     * 退票
     */
    public static  Map<String,Object> refundTickets(Map<String,String> paramMap) throws Exception{
        paramMap.put("note","ddd");
        paramMap.put("type","1");
        paramMap.put("roundTrip","1");
        Map<String,Object> result= HktBusUtil.refundTickets(paramMap);
        return result;
    }

}
